12. Quaternion Exercise

Quaternions

Inscription on the Broom Bridge in Dublin Ireland

Inscription on the Broom Bridge in Dublin Ireland

Quaternions can be hard to get your head around, but you'll see they're worth it when you see how much simpler they are to work with than rotation matrices! To get your energy up for this next exercise, check out this awesome music video about William Rowan Hamilton, the inventor of the quaternion.

Quaternion Exercise

In this exercise, you'll write functions to convert from Euler angles to quaternions and back again. The math to do this is given below:

Quaternion to Euler

\begin{bmatrix} \phi \\ \theta \\ \psi \end{bmatrix} = \begin{bmatrix} \tan^{-1} \frac{2(ab+cd)}{1 - 2(b^2+c^2)} \\ \sin^{-1} 2(ac-db)\\ \tan^{-1} \frac{2(ad+bc)}{1 - 2(c^2+d^2)} \end{bmatrix}

Euler to Quaternion

{\begin{aligned} \begin{bmatrix} a\\ b\\ c\\ d \end{bmatrix} &={\begin{bmatrix}\cos(\psi /2)\\0\\0\\\sin(\psi /2)\end{bmatrix}}{\begin{bmatrix}\cos(\theta /2)\\0\\\sin(\theta /2)\\0\end{bmatrix}}{\begin{bmatrix}\cos(\phi /2)\\\sin(\phi /2)\\0\\0\end{bmatrix}}\\&={\begin{bmatrix}\cos(\phi /2)\cos(\theta /2)\cos(\psi /2)+\sin(\phi /2)\sin(\theta /2)\sin(\psi /2)\\\sin(\phi /2)\cos(\theta /2)\cos(\psi /2)-\cos(\phi /2)\sin(\theta /2)\sin(\psi /2)\\\cos(\phi /2)\sin(\theta /2)\cos(\psi /2)+\sin(\phi /2)\cos(\theta /2)\sin(\psi /2)\\\cos(\phi /2)\cos(\theta /2)\sin(\psi /2)-\sin(\phi /2)\sin(\theta /2)\cos(\psi /2)\end{bmatrix}}\\\end{aligned}}

For additional tips on how you might implement this for your quadcopter in simulation (or in the real world) check out how it's implemented in the Udacidrone API you're using for the projects in this program!

Workspace

This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity, so you may be able to download them there.

Workspace Information:

  • Default file path:
  • Workspace type: jupyter
  • Opened files (when workspace is loaded): n/a